import logging.config
import os


class CusLog(object):
    def __init__(self, log_path, log_filename):

        standard_format = (
            "[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]"
            "[%(levelname)s][%(message)s]"
        )  # 其中name为getlogger指定的名字

        console_format = (
            "[%(asctime)s][%(filename)s:%(lineno)d]"
            "[%(levelname)s][%(message)s]"
        )  # 其中name为getlogger指定的名字

        # simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
        simple_format = "[%(asctime)s][%(levelname)s]%(message)s"

        # 定义日志输出格式 结束
        # print(os.path.dirname(os.path.abspath(__file__)))

        if not os.path.exists(log_path):
            os.makedirs(log_path)

        # if not os.path.exists(logfile_send_dir):
        #     os.makedirs(logfile_send_dir)

        fix_log_file_name = os.path.join(log_path, log_filename)

        # log配置字典
        LOGGING_DIC = {
            "version": 1,
            "disable_existing_loggers": True,
            "formatters": {
                "standard": {"format": standard_format},
                "console": {"format": console_format},
                "simple": {"format": simple_format},
            },
            "filters": {},
            "handlers": {
                # 打印到终端的日志
                "console": {
                    "level": "INFO",
                    "class": "logging.StreamHandler",  # 打印到屏幕
                    "formatter": "console",
                    
                },
                # 打印到文件的日志,收集info及以上的日志
                "default": {
                    "level": "INFO",
                    "class": "logging.handlers.RotatingFileHandler",  # 保存到文件
                    "formatter": "simple",
                    "filename": fix_log_file_name,  # 日志文件
                    "maxBytes": 1024 * 1024 * 1,  # 日志大小 5M
                    "backupCount": 5,
                    "encoding": "utf-8",  # 日志文件的编码，再也不用担心中文log乱码了
                },
            },
            "loggers": {
                # logging.getLogger(__name__)拿到的logger配置
                "dev": {
                    "handlers": [
                        "default",
                        "console",
                    ],  # 这里把上面定义的两个handler都加上，即log数据既写入文件又打印到屏幕
                    "level": "INFO",
                    "propagate": True,  # 向上（更高level的logger）传递
                },
                "prod": {
                    "handlers": [
                        "default",
                        "console",
                    ],  # 这里把上面定义的两个handler都加上，即log数据既写入文件又打印到屏幕
                    "level": "INFO",
                    "propagate": True,  # 向上（更高level的logger）传递
                },
            },
        }

        logging.config.dictConfig(LOGGING_DIC)  # 导入上面定义的logging配置
        self.logger = logging.getLogger("prod")

    def log_info(self, message):
        self.logger.info(message)

    def log_error(self, message):
        self.logger.error(message)
